//
//  BaseCollectionVC.m
//  ocCrazy
//
//  Created by mac on 2018/7/4.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "BaseCollectionVC.h"

#import "UICollectionView+Tool.h"
#import "BaseCollectionViewCell.h"

#import "LCLeftLayout.h"
#import "AFNetworkReachabilityManager.h"

@interface BaseCollectionVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSInteger _page;
}
@property (nonatomic,strong) NSArray <NSArray <NSString *> *> * classStrArr;
@property (nonatomic,strong) NSArray <NSString *> * headerAndFooterArr;
@end

@implementation BaseCollectionVC



- (void)viewDidLoad {
    
    self.classStrArr = [self.collectionInterface classArr];
    [self.collectionView setCellArrForResCell:CellArr(self.classStrArr)];
    if (self.collectionInterface && [self.collectionInterface respondsToSelector:@selector(headerAndFooter)]) {
        self.headerAndFooterArr = [self.collectionInterface headerAndFooter];
           NSString *header = @"";
           NSString *footer = @"";
           if (self.headerAndFooterArr.count > 0) {
               header = self.headerAndFooterArr[0];
           }
           if (self.headerAndFooterArr.count > 1) {
               footer = self.headerAndFooterArr[1];
           }
        [self.collectionView setHeaderWithClass:header withFooter:footer];
    }
   
    
    [super viewDidLoad];
    _page = 1;
    
    
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (![self.flowLayout isKindOfClass:LCLeftLayout.class]) {
        self.flowLayout.minimumLineSpacing = 0;
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
   
}

- (void)repeatGetData{
    _page = 1;
    [self getData:1];
}


- (void)needRefresh{
    
    __weak typeof(self) weakSelf = self;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         __strong typeof(self) strongSelf = weakSelf;
        strongSelf->_page = 1;
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi){
                [self getData:strongSelf->_page];
            }else{
    
                [self.collectionView.mj_header endRefreshing];
            }
        }];
        
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         __strong typeof(self) strongSelf = weakSelf;
        strongSelf->_page++;
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi){
                [self getData:strongSelf->_page];
            }else{
    
                [self.collectionView.mj_footer endRefreshing];
            }
        }];
    }];
}

- (void)reloadCollectionView{
    self.classStrArr = [self.collectionInterface classArr];
      [self.collectionView setCellArrForResCell:CellArr(self.classStrArr)];
    [self.collectionView reloadData];
}

- (void)reloadCollectionViewAndHeaderFooter{
    
    [self reloadCollectionView];

    self.headerAndFooterArr = [self.collectionInterface headerAndFooter];
     NSString *header = @"";
    NSString *footer = @"";
    if (self.headerAndFooterArr.count > 0) {
        header = self.headerAndFooterArr[0];
    }
    if (self.headerAndFooterArr.count > 1) {
        footer = self.headerAndFooterArr[1];
    }
    [self.collectionView setHeaderWithClass:header withFooter:footer];
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *class = GetClassStr(self.classStrArr,indexPath);
    id cell = (id)[collectionView dequeueReusableCellWithReuseIdentifier:class forIndexPath:indexPath];
    BaseCollectionViewCell * baseCell = cell;
    
    [cell setCellEvent:self.cellEvent];
    BaseModel *model = nil;
    if ([self.collectionInterface respondsToSelector:@selector(modelForCell:)])
        model = [self.collectionInterface modelForCell:indexPath];
    else
        model = _dataSouece[indexPath.item];
    baseCell.indexPath = indexPath;
    baseCell.model = model;
  
    return cell;
}

- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString * classStr = GetClassStr(self.classStrArr, indexPath);
    Class class = NSClassFromString(classStr);
    BaseModel *model = nil;
    if ([self.collectionInterface respondsToSelector:@selector(modelForCell:)])
        model = [self.collectionInterface modelForCell:indexPath];
    return [class cellSize:model];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if ([self.collectionInterface respondsToSelector:@selector(sectionCount)]) return [self.collectionInterface sectionCount];
    if (self.dataSouece.count > 0 && [self.dataSouece[0] isKindOfClass:NSArray.class]) {
        return self.dataSouece.count;
    }
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([self.collectionInterface respondsToSelector:@selector(itemsCount:)]) return [self.collectionInterface itemsCount:section];
    if (self.dataSouece.count > 0 && [self.dataSouece[0] isKindOfClass:NSArray.class]) {
        return [self.dataSouece[section] count];
    }
    return self.dataSouece.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if ([self.collectionInterface respondsToSelector:@selector(collectionViewSelectAtIndexPath:)]) return [self.collectionInterface collectionViewSelectAtIndexPath:indexPath];
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableview = nil;
    
    Class Header = NSClassFromString(self.headerAndFooterArr[0]);
    Class Footer = NSClassFromString(self.headerAndFooterArr[1]);
    if (kind == UICollectionElementKindSectionHeader){
        if (Header) {
            if ([self.collectionInterface respondsToSelector:@selector(noHeader)]) {
                           NSArray <NSNumber *> *n = [self.collectionInterface noHeader];
                           if ([n containsObject:@(indexPath.section)]) {
                               return reusableview;
                           }
                       }
             id headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:self.headerAndFooterArr[0] forIndexPath:indexPath];
            [headerView setSection:indexPath.section];
            if ([self.collectionInterface respondsToSelector:@selector(modelForHeader:)]) {
                [headerView setModel:[self.collectionInterface modelForHeader:indexPath.section]];
            }
            [headerView setReusableViewEvent:self.reusableViewEvent];
            reusableview = headerView;
        }
    }else{
        if (Footer) {
             id footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:self.headerAndFooterArr[1] forIndexPath:indexPath];
             [footerView setSection:indexPath.section];
            [footerView setReusableViewEvent:self.reusableViewEvent];
            reusableview = footerView;
        }
    }
    
    return reusableview;
}

-(CGSize)collectionView: (UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection: (NSInteger)section{
    if (self.headerAndFooterArr.count > 0) {
         Class Header = NSClassFromString(self.headerAndFooterArr[0]);
           if ([self.collectionInterface respondsToSelector:@selector(noHeader)]) {
               NSArray <NSNumber *> *n = [self.collectionInterface noHeader];
               if ([n containsObject:@(section)]) {
                   return CGSizeZero;
               }
           }
           if (Header) {
               return [Header viewSize:nil withSection:section];
           }
    }
   
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (self.headerAndFooterArr.count > 1) {
        Class Footer = NSClassFromString(self.headerAndFooterArr[1]);
        if (Footer) {
            return [Footer viewSize:nil withSection:section];
        }
    }
    return CGSizeZero;
}


- (void)detailModelwithArr:(NSArray <__kindof BaseModel *> *)arr withCallBack:(void(^)(void))callBack{
    if (_page == 1) {
        [self.dataSouece removeAllObjects];
    }
    for (__kindof BaseModel *model in arr) {
        [self.dataSouece addObject:model];
    }
    [self.collectionView cc_endReFresh:arr];
    
    if (callBack) {
        callBack();
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)dataSouece{
    if (!_dataSouece) {
        _dataSouece = @[].mutableCopy;
    }
    return _dataSouece;
}

@end
